package com.lzx.iteam.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.baidu.mapapi.map.Text;
import com.lzx.iteam.CRMCreateEditActivity;
import com.lzx.iteam.CRMTextSelectActivity;
import com.lzx.iteam.R;
import com.lzx.iteam.bean.CRMDetailBean;
import com.lzx.iteam.util.StringUtil;
//import jp.sourceforge.qrcode.geom.Line;

import java.util.ArrayList;

import static com.baidu.location.b.k.co;
import static org.apache.http.protocol.HTTP.CR;

/**
 * Created with IntelliJ IDEA.
 * **********************************
 * User: zhangshuai
 * Date: 2016年 08月 02日
 * Time: 下午2:17
 *
 * @QQ : 1234567890
 * **********************************
 */
public class CRMEditAdapter extends BaseAdapter {

    private Context mContext;
    private ArrayList<CRMDetailBean> mData;
    private LayoutInflater mInflater;

    public CRMEditAdapter(Context mContext, ArrayList<CRMDetailBean> mData) {
        this.mContext = mContext;
        this.mData = mData;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {
        CRMDetailBean bean = mData.get(position);
        int type = -1;
        if (bean.getType() == 1 || bean.getType() == -1) {
            type = 0; // 类型
        } else if (bean.getType() == 0 || bean.getType() == 4) {
            type = 2; // edittext
        } else {
            type = 1; // textview
        }
        return type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        EditHolder editHolder = null;
        TextHolder textHolder = null;
        final CRMDetailBean bean = mData.get(position);
        String topTitle = bean.getTopTitle();
        String title = bean.getTitle();
        String content = bean.getContent();
        switch (getItemViewType(position)) {
            case 0:
                if (convertView != null && convertView.getTag().getClass().equals(ViewHolder.class)) {
                    holder = (ViewHolder) convertView.getTag();
                } else {
                    holder = new ViewHolder();
                    convertView = mInflater.inflate(R.layout.crm_item_title_layout, null);
                    holder.mTvTop = (TextView) convertView.findViewById(R.id.crm_edit_item_tv_top);
                    convertView.setTag(holder);
                }
                if (!StringUtil.isEmpty(topTitle)) {
                    holder.mTvTop.setVisibility(View.VISIBLE);
                    holder.mTvTop.setText(bean.getTopTitle());
                } else {
                    holder.mTvTop.setVisibility(View.GONE);
                }
                break;
            case 1:
                if (convertView != null && convertView.getTag().getClass().equals(TextHolder.class)) {
                    textHolder = (TextHolder) convertView.getTag();
                } else {
                    textHolder = new TextHolder();
                    convertView = mInflater.inflate(R.layout.crm_item_text_layout, null);
                    textHolder.mTvTitle = (TextView) convertView.findViewById(R.id.crm_edit_item_tv_title);
                    textHolder.mTvContent = (TextView) convertView.findViewById(R.id.crm_edit_item_tv_content);
                    convertView.setTag(textHolder);
                }
                textHolder.mTvTitle.setText(title);

                if (!StringUtil.isEmpty(content)) {
                    textHolder.mTvContent.setText(content);
                } else {
                    if (bean.getType() == 6) { // 备注
                        textHolder.mTvContent.setHint("选填");
                    } else if (bean.getType() == 8 || bean.getType() == 10) {
                        textHolder.mTvContent.setHint("请选择");
                    } else {
                        int index = bean.getIndex();
                        if (index == -1) {
                            textHolder.mTvContent.setHint("请选择");
                        } else {
                            if (bean.getType() == 2) { // 跟进状态
                                textHolder.mTvContent.setText(CRMTextSelectActivity.status[index]);
                            } else if (bean.getType() == 3) { // 性别
                                textHolder.mTvContent.setText(CRMTextSelectActivity.sex[index]);
                            } else if (bean.getType() == 5) { // 线索来源
                                textHolder.mTvContent.setText(CRMTextSelectActivity.source[index]);
                            } else if (bean.getType() == 7) {
                                textHolder.mTvContent.setText(CRMTextSelectActivity.stage[index]);
                            } else if (bean.getType() == 9) {
                                textHolder.mTvContent.setText(CRMTextSelectActivity.type[index]);
                            } else if (bean.getType() == 11) {
                                textHolder.mTvContent.setText(CRMTextSelectActivity.chance_origin[index]);
                            } else if (bean.getType() == 14) {
                                textHolder.mTvContent.setText(CRMTextSelectActivity.industry[index]);
                            } else if (bean.getType() == 15) {
                                textHolder.mTvContent.setText(CRMTextSelectActivity.client_level[index]);
                            }
                        }
                    }
                }


                break;
            case 2:
                if (convertView != null && convertView.getTag().getClass().equals(EditHolder.class)) {
                    editHolder = (EditHolder) convertView.getTag();
                } else {
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.crm_item_edit_layout, null);
                    editHolder = new EditHolder(convertView);
                    editHolder.mTvTitle = (TextView) convertView.findViewById(R.id.crm_edit_item_tv_title);
                    editHolder.mEdContent = (EditText) convertView.findViewById(R.id.crm_edit_item_ed_content);
                    editHolder.mLayoutLoction = (LinearLayout) convertView.findViewById(R.id.crm_edit_item_loction);
                    convertView.setTag(editHolder);
                }
                editHolder.position = position;
                editHolder.mTvTitle.setText(title);
                if (!StringUtil.isEmpty(content)) {
                    editHolder.mEdContent.setText(content);
                } else {
                    if (bean.getHintType() == 1) {
                        editHolder.mEdContent.setHint("必填");
                    } else {
                        editHolder.mEdContent.setHint("选填");
                    }
                    /*if ("姓名".equals(title) || "手机".equals(title)){
                        editHolder.mEdContent.setHint("必填");
                    }else{
                        editHolder.mEdContent.setHint("选填");
                    }*/
                }
                if (bean.getType() == 4) {
                    editHolder.mLayoutLoction.setVisibility(View.VISIBLE);
                    editHolder.mLayoutLoction.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ((CRMCreateEditActivity) mContext).setLoction(bean.getContent());
                        }
                    });
                } else {
                    editHolder.mLayoutLoction.setVisibility(View.GONE);
                }
                if ("电话".equals(title) || "手机".equals(title) || "邮编".equals(title) || "销售金额".equals(title)) {
                    editHolder.mEdContent.setInputType(InputType.TYPE_CLASS_PHONE);
                } else if ("邮箱".equals(title)) {
                    editHolder.mEdContent.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                }
                /*if ("联系人".equals(title)) {
                    editHolder.mEdContent.setEnabled(false);
                } else {
                    editHolder.mEdContent.setEnabled(true);
                }*/
                if (bean.isNoEdit()){
                    editHolder.mEdContent.setEnabled(false);
                }else{
                    editHolder.mEdContent.setEnabled(true);
                }

                break;
        }
        return convertView;
    }

    /**
     * 标签
     */
    class ViewHolder {
        TextView mTvTop;
    }

    /**
     * textview
     */
    class TextHolder {
        TextView mTvTitle, mTvContent;
    }

    /**
     * edittext
     */
    class EditHolder {
        TextView mTvTitle;
        EditText mEdContent;
        LinearLayout mLayoutLoction;

        int position;

        public EditHolder(View v) {
            mEdContent = (EditText) v.findViewById(R.id.crm_edit_item_ed_content);
            // TODO 设置EditText的监听
            mEdContent.addTextChangedListener(new TextWatcher() {
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                public void afterTextChanged(Editable s) {
                    // TODO 修改数据
                    mData.get(position).setContent(s.toString());
                    ((CRMCreateEditActivity) mContext).updateView(position, s.toString());
                }
            });

        }

    }

}
